package org.codeart.driver.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.codeart.common.constant.DriverCarEnum;
import org.codeart.common.dto.DriverCarBindingRelationship;
import org.codeart.common.dto.DriverUser;
import org.codeart.common.util.R;
import org.codeart.driver.mapper.DriverCarBindingRelationshipMapper;
import org.codeart.driver.mapper.DriverUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author element
 * @description 针对表【tb_driver_car_binding_relationship】的数据库操作Service
 * @createDate 2023-03-08 15:03:50
 */
@Service
public class DriverCarBindingRelationshipService {

    @Autowired
    private DriverCarBindingRelationshipMapper driverCarBindingRelationshipMapper;
    
    @Autowired
    private DriverUserMapper driverUserMapper;

    public boolean bind(DriverCarBindingRelationship driverCarBindingRelationship) {
        // 司机是否重复绑定
        QueryWrapper<DriverCarBindingRelationship> wrapper = new QueryWrapper<>();
        wrapper.lambda()
               .eq(DriverCarBindingRelationship::getDriverId, driverCarBindingRelationship.getDriverId())
               .eq(DriverCarBindingRelationship::getCarId, driverCarBindingRelationship.getCarId())
               .eq(DriverCarBindingRelationship::getBindState, DriverCarEnum.BIND.value());
        DriverCarBindingRelationship one = driverCarBindingRelationshipMapper.selectOne(wrapper);
        if (one != null) {
            return false;
        }

        // 车辆是否重复绑定
        wrapper = new QueryWrapper<>();
        wrapper.lambda()
               .eq(DriverCarBindingRelationship::getCarId, driverCarBindingRelationship.getCarId())
               .eq(DriverCarBindingRelationship::getBindState, DriverCarEnum.BIND.value());
        DriverCarBindingRelationship two = driverCarBindingRelationshipMapper.selectOne(wrapper);
        if (two != null) {
            return false;
        }

        LocalDateTime now = LocalDateTime.now();
        driverCarBindingRelationship.setBindingTime(now).setBindState(DriverCarEnum.BIND.value());
        int i = driverCarBindingRelationshipMapper.insert(driverCarBindingRelationship);
        return i != -1;
    }

    /**
     * 解绑车辆
     */
    public boolean unbind(DriverCarBindingRelationship driverCarBindingRelationship) {
        QueryWrapper<DriverCarBindingRelationship> wrapper = new QueryWrapper<>();
        wrapper.lambda()
               .eq(DriverCarBindingRelationship::getDriverId, driverCarBindingRelationship.getDriverId())
               .eq(DriverCarBindingRelationship::getCarId, driverCarBindingRelationship.getCarId())
               .eq(DriverCarBindingRelationship::getBindState, DriverCarEnum.BIND.value());
        DriverCarBindingRelationship one = driverCarBindingRelationshipMapper.selectOne(wrapper);
        if (one == null) {
            return false;
        }

        LocalDateTime now = LocalDateTime.now();
        one.setBindState(DriverCarEnum.UNBIND.value()).setUnBindingTime(now);
        int i = driverCarBindingRelationshipMapper.updateById(one);
        return i != -1;
    }

    public R<DriverCarBindingRelationship> getDriverCarRelationShipByDriverPhone(String driverPhone) {
        QueryWrapper<DriverUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DriverUser::getDriverPhone, driverPhone);
        DriverUser driverUser = driverUserMapper.selectOne(queryWrapper);
        Long driverId = driverUser.getId();

        QueryWrapper<DriverCarBindingRelationship> wrapper = new QueryWrapper<>();
        wrapper.lambda()
               .eq(DriverCarBindingRelationship::getDriverId, driverId)
               .eq(DriverCarBindingRelationship::getBindState, DriverCarEnum.BIND.value());

        DriverCarBindingRelationship driverCarBindingRelationship = driverCarBindingRelationshipMapper.selectOne(wrapper);
        return R.ok(driverCarBindingRelationship);
    }
}
